package com.neusoft.neuiotms.dcom.repository.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.model.Knowledge;
import com.neusoft.neuiotms.dcom.model.QKnowledge;
import com.neusoft.neuiotms.dcom.model.QKnowledgeComment;
import com.neusoft.neuiotms.dcom.model.QKnowledgeKeywords;
import com.neusoft.neuiotms.dcom.model.QKnowledgeType;
import com.neusoft.neuiotms.dcom.repository.CustomizedKnowledgeRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.JPQLQuery;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedKnowledgeRepositoryImpl extends AbstractRepository implements CustomizedKnowledgeRepository {

    @Override
    public List<Knowledge> search(final Searchable searchable) {
        final QKnowledge q = QKnowledge.knowledge;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<Knowledge> query = this.query().selectFrom(q);
        query.where(where);
        return query.fetch();
    }

    @Override
    public Page<Knowledge> search(final Searchable searchable, final Pageable pageable) {
        final QKnowledge q = QKnowledge.knowledge;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<Knowledge> query = this.query().selectFrom(q);
        query.where(where);
        return super.search(query, pageable);
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {
        final BooleanBuilder where = new BooleanBuilder();
        final QKnowledge q = QKnowledge.knowledge;

        if (!StringUtils.isEmpty(searchable.getStrValue("title"))) {
            where.and(super.containsStr(q.title, searchable, "title"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("id"))) {
            where.and(super.equalsLong(q.id, searchable, "id"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("knowledgeType"))) {
            where.and(super.equalsLong(q.type.id, searchable, "knowledgeType"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("knowledgeTypeCode"))) {
            where.and(super.equalsStr(q.type.code, searchable, "knowledgeTypeCode"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("keyword"))) {
            where.and(super.containsStr(q.keywords.any().name, searchable, "keyword"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("startTimeBegin"))) {
            where.and(super.afterOrEqualsDate(q.createTime, searchable, "startTimeBegin"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("startTimeEnd"))) {
            where.and(super.beforeOrEqualsDate(q.createTime, searchable, "startTimeEnd"));
        }

        //全局
        if (!StringUtils.isEmpty(searchable.getStrValue("globleSearch"))) {
            final BooleanBuilder whereGlobal = new BooleanBuilder();
            whereGlobal.or(super.containsStr(q.title, searchable, "globleSearch"));
            whereGlobal.or(super.containsStr(q.content, searchable, "globleSearch"));
            whereGlobal.or(super.containsStr(q.userName, searchable, "globleSearch"));

            final QKnowledgeType qKnowledgeType = QKnowledgeType.knowledgeType;
            final QKnowledgeComment qknowledgeComment = QKnowledgeComment.knowledgeComment;
            final QKnowledgeKeywords qKnowledgeKeywords = QKnowledgeKeywords.knowledgeKeywords;
            //知识表的左连接
            final JPQLQuery<Long> reQuery = JPAExpressions.select(q.id).from(q)
                    .leftJoin(qKnowledgeType).on(q.type.id.eq(qKnowledgeType.id)).leftJoin(qknowledgeComment)
                    .on(q.comment.any().id.eq(qknowledgeComment.id))
                    .leftJoin(qKnowledgeKeywords).on(q.keywords.any().id.eq(qKnowledgeKeywords.id));

            final BooleanBuilder likewhere =
                    new BooleanBuilder()
                            .or(super.containsStr(qKnowledgeType.name, searchable, "globleSearch"))
                            .or(super.containsStr(qknowledgeComment.content, searchable, "globleSearch"))
                            .or(super.containsStr(qKnowledgeKeywords.name, searchable, "globleSearch"));

            reQuery.where(likewhere);
            whereGlobal.or(QKnowledge.knowledge.id.in(reQuery));
            where.and(whereGlobal);
        }

        return where;
    }

    @Override
    protected Class<?> getModelClass() {
        return Knowledge.class;
    }

}
